package com.fafa.swing.base;


import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;

/**
 * @author : fafa
 * @version V1.0
 * @Project: SwingTest
 * @Package com.fafa.swing
 * @Description: JListDemo2 多选
 * @date Date : 2019年11月24日 16:34
 */
public class JListDemo2 extends JFrame {

    public JListDemo2(){
        setTitle("列表框组件");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(300,300,400,200);
        JPanel contentPanel = new JPanel();   //内容面板
        contentPanel.setBorder(new EmptyBorder(5,5,5,5));
        contentPanel.setLayout(new BorderLayout(0,0));
        setContentPane(contentPanel);   //应用内容面板
        JScrollPane scrollPane = new JScrollPane();
        contentPanel.add(scrollPane, BorderLayout.CENTER);
        JList jList = new JList();
        //限制只能选择一个元素
        jList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        scrollPane.setViewportView(jList);
        String[] listData = new String[12];
        for (int i = 0; i < listData.length; i++) {
            listData[i] = "这是列表框的第 "+i+" 个元素~";
        }
        jList.setListData(listData);
        setVisible(true);
    }

    public static void main(String[] args) {
        JListDemo2 jListDemo2 = new JListDemo2();
    }
}
